package org.nanotek.util.text;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * Given a string and a formula will apply the formula for all 
 * occurrences over the String. 
 * 
 * @author jose.carlos.canova@gmail.com
 *
 */
@SuppressWarnings("serial")
public class NameNormalizer implements Normalizer<String, String,Number> {

	private Number id; 
	private String formula; 
	private String replaceMent;
	
	public NameNormalizer()
	{}
			
	public NameNormalizer(String formula, String replaceMent) {
		super();
		this.formula = formula;
		this.replaceMent = replaceMent;
	}

	public NameNormalizer(String formula, String replaceMent,Number id) {
		super();
		this.formula = formula;
		this.replaceMent = replaceMent;
		this.id = id;
	}
	

	@Override
	public void setId(Number id) {
		this.id = id; 
	}

	@Override
	public Number getId() {
		return this.id;
	}
	
	public String getFormula() {
		return formula;
	}

	public void setFormula(String formula) {
		this.formula = formula;
	}

	public String getReplaceMent() {
		return replaceMent;
	}

	public void setReplaceMent(String replaceMent) {
		this.replaceMent = replaceMent;
	}
	
	@Override
	public String normalize(String value) {
		String matchValue = value;
		Pattern pattern = Pattern.compile(formula);
		Matcher matcher = pattern.matcher(matchValue);
		if (matcher.find()){ 
			matchValue = matcher.replaceAll(replaceMent);
		}
		return matchValue;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((formula == null) ? 0 : formula.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((replaceMent == null) ? 0 : replaceMent.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		NameNormalizer other = (NameNormalizer) obj;
		if (formula == null) {
			if (other.formula != null) {
				return false;
			}
		} else if (!formula.equals(other.formula)) {
			return false;
		}
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (replaceMent == null) {
			if (other.replaceMent != null) {
				return false;
			}
		} else if (!replaceMent.equals(other.replaceMent)) {
			return false;
		}
		return true;
	}

	
}
